package com.smart.platform.controller;

import com.smart.platform.common.Result;
import com.smart.platform.common.ocr.OcrOnlyText;
import com.smart.platform.common.ocr.OcrParam;
import com.smart.platform.service.Ocrservice;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/text")
//@Api(tags = "Ocr单独访问接口")
public class OcrController {

    @Autowired
    private Ocrservice ocrservice;

    /**
     * ocr调用接口
     * @param ocrParam
     * @return
     */
    @ApiOperation(value = "Ocr访问接口", notes = "需要图片的url")
    @PostMapping("/text-only")
    public Result<OcrOnlyText> textOnly(@RequestBody OcrParam ocrParam){
        OcrOnlyText ocrOnlyText = new OcrOnlyText();
        //1.执行servce方法获得OCR数据
        List<OcrOnlyText> ocrTexts = ocrservice.ocrText(ocrParam.getImgPath());
        List<String> texts = new ArrayList<String>();
        for (int i = 0; i < ocrTexts.size(); i++) {
            texts.add(ocrTexts.get(i).getText());
        }
        //将返回的OCR数据转化为字符串
        String str = String.join(" ",texts);
        ocrOnlyText.setText(str);
        return Result.success(ocrOnlyText);
    }


}
